(function (a2dmah5) {
    a2dmah5.addExpandPage(0, function (get) {
        const Common = get('UtilsCommon', 'Common');

        return {
            expandInit() {
                this.saveEntityData2 = this.getProperty('saveEntityData2');
                const entityEdit2 = this.getProperty('entityEdit2');

                entityEdit2.properties.forEach(p => {
                    if (p.name === 'InstallAmount') { this.installAmountProperty = p; p.valueChange = this.setTotalAmount.bind(this); }
                    else if (p.name === 'FreightAmount') { this.freightAmountProperty = p; p.valueChange = this.setTotalAmount.bind(this); }
                    else if (p.name === 'OtherAmount') { this.otherAmountProperty = p; p.valueChange = this.setTotalAmount.bind(this); }
                    else if (p.name === 'DiscountAmount') { this.discountAmountProperty = p; p.valueChange = this.setTotalAmount.bind(this); }
                    else if (p.name === 'TotalAmount') { this.totalAmountProperty = p; p.valueChange = this.setTotalAmount.bind(this); }
                    else if (p.name === 'Details') this.detailsProperty = p;
                });
            },
            expandSetEntityData({ entityData }) {
                entityData.Status = 0;
                entityData.SaleRowVersion = this.entityData.SaleRowVersion;
                entityData.SaleOrderId = this.entityData.SaleOrderId;
                return entityData;
            },
            expandDataLoad({ data }) {
                this.entityData = data;
                if ((data.Status == 0 || data.Status == 1) && Common.isEquals(data.CreateUser, this.loginUser.UserId, true)) this.saveEntityData2.setIsVisible(true);
            },
            setRowDataProperties2({view,data}){
                view.properties.forEach(p => {
                    if (p.name === 'Num') {
                        if (data.BalanceNum > 0) {
                            p.maxValue = data.BalanceNum;
                            p.minValue = 0;
                        }
                        else {
                            p.minValue = data.BalanceNum;
                            p.maxValue = 0;
                        }
                    }
                });
            },
            setRowDataProperties({ view, data }) {
                view.properties.forEach(p => {
                    if (p.name === 'Num') {
                        if (data.BalanceNum > 0) {
                            p.maxValue = data.BalanceNum;
                            p.minValue = 0;
                        }
                        else {
                            p.minValue = data.BalanceNum;
                            p.maxValue = 0;
                        }
                    }
                    if (p.name === 'InstallNum' || p.name === 'InstallAmount') p.valueChange = this.inputValueChange2.bind(this);
                });
            },
            inputValueChange2(value, property, view) {
                if (!property.isChanged) return;
                this.computeAmount(view);
                property.isChanged = false;
            },
            detailsChange(dataList) {
                let installAmount = 0;
                dataList = dataList || [];

                dataList.forEach(d => {
                    installAmount += Common.enlarge(d.Amount2);
                });

                installAmount = installAmount / 100;

                this.installAmountProperty.setValue(installAmount);
                this.setTotalAmount(null, null, null, installAmount);
            },
            setTotalAmount(value, property, view, installAmount) {
                if (property && !property.isChanged) return;
                if (installAmount === undefined) installAmount = Common.enlarge(this.installAmountProperty.getValue());
                else installAmount = Common.enlarge(installAmount);

                let freightAmount = Common.enlarge(this.freightAmountProperty.getValue());
                let otherAmount = Common.enlarge(this.otherAmountProperty.getValue());
                let discountAmount = Common.enlarge(this.discountAmountProperty.getValue());

                let totalAmount = installAmount + freightAmount + otherAmount - discountAmount;
                totalAmount = totalAmount / 100;
                this.totalAmountProperty.setValue(totalAmount);
            },
            computeAmount2(view) {
                let price = 0;
                let num = 0;
                let amount = 0;
                let amountProperty;
                view.properties.forEach(p => {
                    if (p.name === 'Num') num = Common.getFloat(p.data.Num);
                    else if (p.name === 'InstallAmount') price = Common.getFloat(p.data.InstallAmount);
                    else if (p.name === 'Amount') amountProperty = p;
                });

                let a = Common.enlarge(price * num, 2);
                amount = a / 100;
                amountProperty.setValue(amount);
                amountProperty.data.Amount = amount;
                this.detailsChange(this.detailsProperty.getValue());
            }
        }
    });

})(window.a2dmah5);